<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
use Common\Model\AreaModel;
use Common\Model\ColumnLiveAdminModel;

class AreaController extends AdminbaseController
{
	protected $area_model;
    function _initialize()
    {
        parent::_initialize();
        $this->area_model = D("Common/Area");
    }

    
    /**
     * 地区列表
     */
    function index()
    {
        $formParam['areaName'] = I('param.areaName', '');
        $this->assign("formParam", $formParam);

        $currentPage = I('param.p', 0);
        if ($currentPage <= 0) {
            $currentPage = 1;
        }
        
        $pageSize = '20';
        $pageStart = ($currentPage - 1) * $pageSize;
        $pageEnd = $currentPage * $pageSize;
        $result = $this->area_model->getEstablishList($formParam['areaName'],$pageStart,$pageSize);

        //分页
        $page = $this->page($result['count_Total'], 20);
        $this->assign("page", $page->show('Admin'));

        
        $count_data = array();
        $count = 0;
        $count_total = 0;
  		foreach($result['data'] as $key=>$value){
  			if($key !='0'){
  				if($old_initial !=$value['initial']){
  					$count_data[] = $count;
  					$count_total +=$count;
  					$count = 0;
  				}
  			}
  			$old_initial = $value['initial'];
  			$count++;
  			
  			//管理员
  			$result['data'][$key]['administrator_str'] = implode(',', $this->area_model->getAdministrator($value['area_id']));
  		}
  		$count_data[] = 20 - $count_total;

  		$this->assign("count_data", $count_data);
        $this->assign("data", $result['data']);
        $this->display();
    }
    
    
    /**
     * 地区开启
     */
    public function open($id)
    {
    	if ($id > 0) {
    		$result = $this->area_model->setDisableStatus($id, 0);
    		if ($result) {
    			$this->success("修改成功");
    		} else {
    			return $this->error($result['Message']);
    		}
    	} else {
    		 $this->error("参数错误！");
    	}
    }
    
    /**
     * 地区禁用
     */
    public function close($id)
    {
    	if ($id > 0) {
    		$result = $this->area_model->setDisableStatus($id, 1);
    		if ($result) {
    			$this->success("修改成功");
    		} else {
    			return $this->error($result['Message']);
    		}
    	} else {
    		 $this->error("参数错误！");
    	}
    }
    
    
    /**
     * 地区删除
     */
    function del()
    {
    	$areaId = I('param.areaId', "");
    	if (empty($areaId)) {
    		$this->error("参数错误！");
    	}
    	$data['establish_status'] = '0';
    	$result = $this->area_model->areaDel($areaId);
    	if ($result) {
    		$this->success("删除成功！");
    	} else {
            $this->error("删除失败！");
    	}
    }
    
	
    /**
     * 进入添加地区页面
     */
    function areaAdd()
    {
    	$this->assign("Title", "地区");
    	//所有地区列表
    	$province = M('vrnews1.provinces');
        $city     = M('vrnews1.cities');
        $province_list = $province->select();

//        var_dump($province_list);
        $GLOBALS['$province_list'] = $province_list;
    	//管理员列表
    	$administrator_list = $this->area_model->getAdministrator();

    	$this->assign("province_list", $province_list);
    	$this->assign("administrator_data", $administrator_list);
    	$this->display('areaadd');
    }

    /**
     * 获取省份下所有城市
     */
    public function getCityList(){
        $provinceid = I("provinceId");
        $city     = M('vrnews1.cities');
        $city_list = $city->where(['provinceid'=>$provinceid])->select();
        exit(json_encode($city_list));
    }

    /**
     * 检查城市是否已经开通
     */
    public function checkCity(){
        $city = I('city');
        $area = M('vrnews1.area');
        $is_have = $area->where(['name'=>$city, 'establish_status'=>1])->select();
        if($is_have){
            echo 1;exit();
        }else{
            echo 0;exit();
        }
    }
    /**
     * 进入修改管理员页面
     */
    function editAdmin()
    {
    	$area_id = I("param.area_id", "");

    	if (empty($area_id)) {
            $this->error("参数错误！");
        }
        
    	//所有当前地区的管理员列表
    	$administratored_list = $this->area_model->getAdministrator($area_id);
    	 
    	//所有管理员列表
    	$administrator_list = $this->area_model->getAdministrator();
    
    	$this->assign("area_id", $area_id);
    	$this->assign("administratored_data", $administratored_list);
    	$this->assign("administrator_data", $administrator_list);
    	$this->display('editadmin');
    }

    
    
    /**
     * 提交
     */
    function areaPost()
    {
        $city = I('city');
        $company_name = I('company_name');
        $ename = I('ename');
        $adminstract_arr = I('adminstract_arr');
        $province = I('province');
//    	$area_ids =  rtrim(I("param.area_hidden_ids", ""),',');
//    	$administrator_ids = rtrim(I("param.administrator_hidden_ids", ""),',');
        if(!$city || !$province || !$ename || !$company_name){
            echo -1;exit;
        }
        
        //设置地区开通
        $data['name'] = $city;
        $data['establish_status'] = '1';
        $data['disable_status'] = '0';
        $data['initial'] = strtoupper(substr($ename,0,1));
        $data['pinyin'] = $ename;
        $rid = $this->area_model->add($data);

        //开通公司信息
        $company = M('vrnews1.company');
        $company_info['name'] = $company_name;
        $company_info['prov'] = $province;
        $company_info['city'] = $city;
        $company_info['create_time'] = time();
        $company->add($company_info);
        
        //设置管理员
        if(count($adminstract_arr) > 0){
            $admin_user = M('vradmin1.admin_user', null);
        	for($i=0;$i<count($adminstract_arr);$i++){
                $admin_info = $admin_user->where(['username' => trim($adminstract_arr[$i]), 'status'=>1, 'user_type'=>1])->find();
                $dataList[] = array('area_id'=>$rid,'admin_id'=>$admin_info['admin_id']);
        	}
        	$admin_model = M('vrnews1.admin','area_');
        	$rid = $admin_model->addAll($dataList);
        }
        
        
       if (!empty($rid)) {
            echo 1;exit();
//            $this->success($successInfo, U("Admin/Area/index"));
        } else {
            echo 0;exit();
        }
    }

    /**
     * 根据关键字查找相关管理员账号
     */
    public function findAdmin(){
        $key_word = I('find');
        $admin_user = M("vradmin1.admin_user", null);
        $data['username'] = array('like', '%'.$key_word.'%');
        $data['status'] = 1;
        $admin_list = $admin_user->where($data)->select();
        echo json_encode($admin_list);exit();
    }
    /**
     * 修改地区管理员提交
     */
    function editAdminPost()
    {
    	$result = '0';
    	$area_id = I("param.area_id", "");
    	$administrator_ids = rtrim(I("param.administrator_hidden_ids", ""),',');
    	
    	if (empty($area_id)) {
    		echo $result;exit;
    	}
    	
    	
    	//设置管理员
    	if($administrator_ids){
    		$administrator_arr = explode(',', $administrator_ids);

    		foreach($administrator_arr as $administrator_key=>$administrator_value){
    			$dataList[] = array('area_id'=>$area_id,'admin_id'=>$administrator_value);
                $dataAdminids[] = $administrator_value;
    		}
    		$admin_model = M('vrnews1.admin','area_');
    		$admin_model->where('area_id = '.$area_id)->delete();
    		if($admin_model->addAll($dataList)){
                //查看 对应的 直播权限管理员
                $live_admin = new ColumnLiveAdminModel();
                $live_admins = $live_admin->where("column_id = ".$area_id. " and type = 1")->select();
                //处理 对应的直播权限管理人
                foreach ($live_admins as $key=>$val){
                    if(!in_array($val['admin_id'], $dataAdminids)){
                        $live_admin->where("id = ".$val['id'])->delete();
                    }
                }
            }
            $result = '1';
    	}
    	echo $result;
    }


    //显示 修改直播权限 弹窗
    public function editliverole(){
        $area_id     = I("param.area_id", "");
        $live_status = I("param.live_status");

        if (empty($area_id)) {
            $this->error("参数错误！");
        }

        //所有当前地区的管理员列表
        $administratored_list = $this->area_model->getAdministrator($area_id);

        //查看 对应 直播管理权限
        $live_column_admins = new  ColumnLiveAdminModel();
        $live_admins = $live_column_admins->alias("a")
            ->join("vradmin1.admin_user b on a.admin_id = b.admin_id")
            ->where("a.column_id = $area_id and a.type = 1")
            ->field("a.admin_id,b.username")
            ->select();

        //当前 拥有直播权限的人
        foreach ($live_admins as $key=>$val){
            $live_arr[$key]     = $val['username'];
            $live_ids_arr[$key] = $val['admin_id'];
        }
        //关闭状态 为空
        if($live_status == 0){
            $live_arr = '';
            $live_ids_arr = '';
        }

        $this->assign("column_id", $area_id);
        $this->assign("column_type", 1);
        $this->assign("arr_mer", $administratored_list);
        $this->assign("live_arr", $live_arr);
        $this->assign("live_ids_arr", $live_ids_arr);
        $this->assign("live_status", $live_status);
        $this->display('editliverole');
    }

    //修改直播权限
    public function liverolePost(){
        $result = '0';
        $column_id = I("param.column_id", "");
        $live_status = I("param.live_status");
        $admin_ids = rtrim(I("param.admin_hidden_ids", ""),',');

        if (empty($column_id)) {
            echo $result;exit;
        }
        if($live_status == 0){
            //关闭
            //修改 栏目直播状态
            $news_column = new AreaModel();
            $data['live_status'] = 0;
            $news_column->where('area_id = '.$column_id)->data($data)->save();

            $admin_model = new ColumnLiveAdminModel();
            $columns_sel = $admin_model->where("column_id = $column_id and type = 1")->select();

            if($columns_sel){
                $admin_model->where('column_id = '.$column_id.' and type = 1')->delete();
                echo 1;
            }else{
                echo $result;
            }
        }else{
            //开启
            //修改 栏目直播状态
            $news_column = new AreaModel();
            $data['live_status'] = 1;
            $news_column->where('area_id = '.$column_id)->data($data)->save();


            //设置管理员
            if($admin_ids){
                $admin_arr = explode(',', $admin_ids);

                foreach($admin_arr as $admin_key=>$admin_value){
                    $dataList[] = array('column_id'=>$column_id,'admin_id'=>$admin_value,'type'=>1);
                }

                $admin_model = new ColumnLiveAdminModel();
                $columns_sel = $admin_model->where("column_id = $column_id and type = 1")->select();

                if($columns_sel){
                    $admin_model->where('column_id = '.$column_id.' and type = 1')->delete();
                }
                if($admin_model->addAll($dataList)) echo 1;
            }else{
                echo '0';
            }
        }
    }

    /**
     * 
     */
    function NewsDetail($newsId)
    {
        $this->assign("Title", "新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        $this->assign("news", $news);
        $this->display('newsdetail');
    }
    #endregion

    #region 专题新闻
    function SubNewsAdd()
    {
        $this->assign("Title", "专题新闻");
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $subject = $this->getSubject();
        $this->assign("subject", $subject);
        $this->display('subnewsadd');
    }

    function SubNewsEdit($newsId)
    {
        $this->assign("Title", "专题新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);

        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $subject = $this->getSubject();
        $this->assign("subject", $subject);

        $this->display('subnewsedit');
    }

    function SubNewsPost()
    {
        $newsId = I("param.newsId", '');
        $newsType = 3;
        $newsKey = I("param.newsKey", '');
        $newsTitle = I("param.newsTitle", '');
        $newsVeneto = I("param.newsVeneto", '');
        $newsSource = I("param.newsSource", '');
        $imageUrl = I("param.imageUrl", '');
        $subject = I("param.subject", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");
        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsVeneto)) {
            $this->error("请填写新闻摘要！");
        }
        if (empty($newsSource)) {
            $this->error("请填写新闻来源！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传新闻图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }
        $newsContent = $_POST["newsContent"];
        if (empty($newsContent)) {
            $this->error("新闻正文不能为空！");
        }
        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&titleImg=' . $imageUrl . '&keyWords=' . $newsKey . '&summary=' . $newsVeneto . '&areaId=' . $city . '&source=' . $newsSource . "&title=" . $newsTitle . "&newsContent=" . $newsContent . "&parentId=" . $subject;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function SubNewsDetail($newsId)
    {
        $this->assign("Title", "新闻");
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $this->assign("news", $news);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        /*print_r($news);
        die();*/
        $this->display('subnewsdetail');
    }

    #endregion

    #region 图片新闻
    function PictureAdd()
    {
        $this->assign("Title", "图片新闻");
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('pictureadd');
    }

    function PictureEdit($newsId)
    {
        $this->assign("Title", "图片新闻");
        $data = $this->curl_http(C('news_url') . "newsPic/getNewsPicList?curPage=1&pageSize=1000&newsId={$newsId}");
        $imgData = $data['ReturnData']['data'];
        $this->assign("imgData", $imgData);

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $this->assign("news", $news);
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('pictureedit');
    }

    function PictureDetail($newsId)
    {
        $this->assign("Title", "图片新闻");
        $data = $this->curl_http(C('news_url') . "newsPic/getNewsPicList?curPage=1&pageSize=1000&newsId={$newsId}");
        $imgData = $data['ReturnData']['data'];
        $this->assign("imgData", $imgData);

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }

        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        $this->assign("news", $news);


        $this->display('picturedetail');
    }

    function PicturePost()
    {
        $newsType = 2;
        $newsKey = I("param.newsKey", '');
        $newsTitle = I("param.newsTitle", '');
        $newsSource = I("param.newsSource", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");

        $pictureTag = $_POST['pictureTag'];
        $imageUrl = $_POST['imageUrl'];
        $pictureDesc = $_POST['pictureDesc'];
        $pictureCount = I("param.pictureCount", 0);

        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsSource)) {
            $this->error("请填写新闻来源！");
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if ($pictureCount <= 0) {
            $this->error("请上传图片！");
        }
        if (count($pictureTag) - 1 != $pictureCount) {
            $this->error("请全部填写图片标签！");
        }
        if (count($imageUrl) - 1 != $pictureCount) {
            $this->error("您有未上传的图片！");
        } else {
            foreach ($imageUrl as $k => $v) {
                $imageUrl[$k] = "http://{$_SERVER['HTTP_HOST']}" . $v;
            }
        }
        if (count($pictureDesc) - 1 != $pictureCount) {
            $this->error("请全部填写图片描述！");
        }
        $titleArr=array();
        $usePicNum = 3;
        if ($pictureCount< $usePicNum) {
            $usePicNum = $pictureCount;
        }else{
            $usePicNum=3;
        }
        for ($i = 0; $i < $usePicNum; $i++) {
            $titleArr[] = $imageUrl[$i];
        }
        $fields = '&newsType=' . $newsType . '&keyWords=' . $newsKey . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&areaId=' . $city . '&source=' . $newsSource . "&title=" . $newsTitle . "&newsContent=内容&titleImg=" . implode(',',$titleArr);
        $successInfo = "添加成功";
        $fields = $fields . "&creator=" . $this->getAdminName();
        $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        if ($result['Success']) {
            $newsId = $result['ReturnData']["data"]["newsId"];
            if (!empty($newsId)) {
                for ($i = 0; $i < $pictureCount; $i++) {
                    $currentTag = $pictureTag[$i];
                    $currentImg = $imageUrl[$i];
                    $currentDesc = $pictureDesc[$i];
                    $currentField = "&newsId={$newsId}&imgDesc={$currentDesc}&title={$currentTag}&imgUrl={$currentImg}&creator=" . $this->getAdminName();
                    $this->curl_http(C('news_url') . 'newsPic/addNewsPic?token=' . $_SESSION["TOKEN"], $currentField);
                }
            }
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }
    #endregion

    #region 视频新闻

    function VideoAdd($newsType)
    {
        if (!in_array($newsType, array(4, 5, 6))) {
            $this->error("类型错误！");
        }
        $this->assign("newsType", $newsType);
        //4视频新闻；5直播新闻；6推广
        $title = "视频新闻";
        if ($newsType == 5) {
            $title = "直播新闻";
        } elseif ($newsType == 6) {
            $title = "推广新闻";
        }
        $this->assign("Title", $title);
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('videoadd');
    }

    function VideoEdit($newsType, $newsId)
    {
        if (!in_array($newsType, array(4, 5, 6))) {
            $this->error("类型错误！");
        }
        $this->assign("newsType", $newsType);
        $title = "视频新闻";
        if ($newsType == 5) {
            $title = "直播新闻";
        } elseif ($newsType == 6) {
            $title = "推广新闻";
        }
        $this->assign("Title", $title);

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        // print_r($news);
        //die();
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);

        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('videoedit');
    }

    //新闻类型：1普通新闻；2图片新闻；3专题新闻；4视频新闻；5直播新闻；6推广视频；7其它;8专题
    function VideoPost()
    {
        $newsId = I("param.newsId", '');
        $newsTitle = I("param.newsTitle", '');
        $newsType = I("param.newsType", '');
        $newsKey = I("param.newsKey", '');
        $newsPath = I("param.newsPath", '');
        $newsAuthor = I("param.newsAuthor", '');
        $imageUrl = I("param.imageUrl", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category", "");
        $tags = I("param.tags", "");
        if (!in_array($newsType, array(4, 5, 6))) {
            $this->error("类型错误！");
        }
        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsKey)) {
            $this->error("请填写关键字！");
        }
        if (empty($newsPath)) {
            $this->error("请填写路径！");
        }
        if (empty($newsAuthor)) {
            $this->error("请填写作者！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传新闻图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }
        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&subjectTagId=' . $tags . '&titleImg=' . $imageUrl . '&keyWords=' . $newsKey . '&linkUrl=' . $newsPath . '&areaId=' . $city . '&author=' . $newsAuthor . "&newsContent=内容&title=" . $newsTitle;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function VideoDetail($newsId)
    {
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $news['newsTag'] =  $this->getTagsValue($news['categoryId'], $news['subjectTagId']);
        $this->assign("news", $news);

        $newsType = $news['newsType'];
        $title = "视频新闻";
        if ($newsType == 5) {
            $title = "直播新闻";
        } elseif ($newsType == 6) {
            $title = "推广新闻";
        }
        $this->assign("Title", $title);
        $this->display('videodetail');
    }
    #endregion

    #region 专题

    function SubjectAdd()
    {
        $this->assign("Title", "专题");
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('subjectadd');
    }

    function SubjectEdit($newsId)
    {
        $this->assign("Title", "专题");

        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $this->assign("news", $news);
        //print_r($news);die();
        $province = $this->getProvince();
        $this->assign("province", $province);
        $category = $this->getCategory();
        $this->assign("category", $category);
        $this->display('subjectedit');
    }

    function SubjectPost()
    {
        $newsId = I("param.newsId", '');
        $newsType = 8;
        $newsTitle = I("param.newsTitle", '');
        $newsAuthor = I("param.newsAuthor", '');
        $newsVeneto = I("param.newsVeneto", '');
        $imageUrl = I("param.imageUrl", '');
        $subImageUrl = I("param.subImageUrl", '');
        $province = I("param.province", "");
        $city = I("param.city", "");
        $category = I("param.category");

        if (empty($newsTitle)) {
            $this->error("请填写新闻标题！");
        }
        if (empty($newsAuthor)) {
            $this->error("请填写作者！");
        }
        if (empty($newsVeneto)) {
            $this->error("请填写新闻摘要！");
        }
        if (empty($imageUrl)) {
            $this->error("请上传标题图片！");
        } else {
            if (strpos($imageUrl, "http://") === false) {
                $imageUrl = "http://{$_SERVER['HTTP_HOST']}" . $imageUrl;
            }
        }
        if (empty($subImageUrl)) {
            $this->error("请上传专题图片！");
        } else {
            if (strpos($subImageUrl, "http://") === false) {
                $subImageUrl = "http://{$_SERVER['HTTP_HOST']}" . $subImageUrl;
            }
        }
        /*if ($province <= 0) {
            $this->error("请选择所属省！");
        }
        if ($city <= 0) {
            $this->error("请选择所属城市！");
        }*/
        if (empty($category)) {
            $this->error("请选择分类！");
        }

        $fields = '&newsType=' . $newsType . '&categoryId=' . $category . '&titleImg=' . $imageUrl . '&subjectImg=' . $subImageUrl . '&areaId=' . $city . '&author=' . $newsAuthor . "&summary=" . $newsVeneto . "&newsContent=内容&title=" . $newsTitle;
        if (!empty($newsId)) {
            $successInfo = "修改成功";
            $fields = $fields . "&newsId=" . $newsId . "&modifier=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/modifyNews?token=' . $_SESSION["TOKEN"], $fields);
        } else {
            $successInfo = "添加成功";
            $fields = $fields . "&creator=" . $this->getAdminName();
            $result = $this->curl_http(C('news_url') . 'news/addNews?token=' . $_SESSION["TOKEN"], $fields);
        }
        if ($result['Success']) {
            $this->success($successInfo, U("Admin/News/index"));
        } else {
            $this->error($result['Message']);
        }
    }

    function SubjectDetail($newsId)
    {
        $news = $this->getNews($newsId);
        if (!is_array($news)) {
            $this->error("查找新闻失败！");
        }
        $news['provinceId'] = $this->getProvinceId($news['areaId']);
        $news['city'] = $this->getAreaNmae($news['areaId']);
        $news['province'] = $this->getAreaNmae($news['provinceId']);
        $this->assign("news", $news);

        $this->assign("Title", '专题');
        $this->display('subjectdetail');
    }

    #endregion

    #region 帮助方法

    function getAdminName()
    {
        return "admin";
    }

    function getProvince()
    {
        $data = $this->curl_http(C('news_url') . 'area/getAreaListA?lId=1');
        $data = $data['ReturnData']['data'];
        //print_r($data);
        //die();
        return $data;
    }

    function getProvinceId($aId)
    {
        $data = $this->curl_http(C('news_url') . 'area/getAreaListA?aId=' . $aId);
        $data = $data['ReturnData']['data'][0]['pId'];
        return $data;
    }

    function getAreaNmae($aId)
    {
        $data = $this->curl_http(C('news_url') . 'area/getAreaListA?aId=' . $aId);
        $data = $data['ReturnData']['data'][0]['aN'];
        return $data;
    }

    function getCity($pid)
    {
        $data = $this->curl_http(C('news_url') . "area/getAreaListA?lId=2&pId={$pid}");
        $data = $data['ReturnData']['data'];
        $result['status'] = 1;
        $result['data'] = $data;
        $this->ajaxReturn($result);
    }

    function getCategory()
    {
        $data = $this->curl_http(C('news_url') . 'newsCategory/getNewsCategoryList');
        $data = $data['ReturnData']['data'];
        return $data;
    }

    function getTags($cId)
    {
        $data = $this->curl_http(C('news_url') . "newsTag/getNewsTagList?categoryId={$cId}&curPage=1&pageSize=1000");
        $data = $data['ReturnData']['data'];
        $result['status'] = 1;
        $result['data'] = is_array($data) ? $data : array();
        $this->ajaxReturn($result);
    }

    function getTagsValue($cId, $subId)
    {
        $value = "";
        $data = $this->curl_http(C('news_url') . "newsTag/getNewsTagList?categoryId={$cId}&curPage=1&pageSize=1000");
        $data = $data['ReturnData']['data'];
        foreach ($data as $k => $v) {
            if ($v['tagId'] == $subId) {
                $value = $v['tagName'];
                break;
            }
        }
        return $value;
    }

    function getNews($newId)
    {
        $data = $this->curl_http(C('news_url') . "news/getNewsListA?&newsId={$newId}&&curPage=1&pageSize=10");
        $data = $data['ReturnData']['data'];
        //print_r($data[0]);die();
        return $data[0];
    }

    function getSubject()
    {
        $data = $this->curl_http(C('news_url') . "news/getNewsListA?&newsType=8&&curPage=1&pageSize=10");
        $data = $data['ReturnData']['data'];
        //print_r($data);die();
        return $data;
    }

    function getNewsType($newsType)
    {
        $returnArr = array();
        //1普通新闻；2图片新闻；3专题新闻；4视频新闻；5直播新闻；6推广视频；7其它;8专题
        switch ($newsType) {
            case 1:
                $returnArr = array("title" => "新闻", "edit_url" => "NewsEdit", "detail_url" => "NewsDetail");
                break;
            case 2:
                $returnArr = array("title" => "图片新闻", "edit_url" => "PictureEdit", "detail_url" => "PictureDetail");
                break;
            case 3:
                $returnArr = array("title" => "专题新闻", "edit_url" => "SubNewsEdit", "detail_url" => "SubNewsDetail");
                break;
            case 4:
                $returnArr = array("title" => "视频新闻", "edit_url" => "VideoEdit", "detail_url" => "VideoDetail");
                break;
            case 5:
                $returnArr = array("title" => "直播新闻", "edit_url" => "VideoEdit", "detail_url" => "VideoDetail");
                break;
            case 6:
                $returnArr = array("title" => "推广新闻", "edit_url" => "VideoEdit", "detail_url" => "VideoDetail");
                break;
            case 8:
                $returnArr = array("title" => "专题", "edit_url" => "SubjectEdit", "detail_url" => "SubjectDetail");
                break;
        }
        return $returnArr;
    }

    #endregion

}